package at.codebase.web.servlets.admin;

import java.util.ArrayList;
import java.util.List;

import at.codebase.core.log.Log;
import at.codebase.db.dao.EApiAccessDao;
import at.codebase.db.dao.EApiPartnerDao;
import at.codebase.db.entities.EApiAccess;
import at.codebase.db.entities.EApiPartner;
import at.codebase.web.templates.MainTemplate;
import at.codebase.web.util.LocalRequest.RequestInformation;
import at.codebase.web.util.URLParameters.ParameterException;
import at.codebase.web.util.servlet.SecureServlet;
import at.codebase.web.util.template.Template;
import at.codebase.web.util.template.TemplateEngine;

public class ManageApiAccess  extends SecureServlet {

	@Override
	protected void initSecurityLevels(ArrayList<UserLevel> secLevels) {
		secLevels.add(UserLevel.ADMIN);
	}

	@Override
	protected void processRequestSecurityLevel(RequestInformation requestInfo, UserLevel level, RequestType requestType) {
		
		if(requestType.equals(RequestType.POST)) {
			if(!tokenCorrect(requestInfo)) {
				processRequestNoAccess(requestInfo, level);
				return;
			}
			
			try {
				String action = requestInfo.getParameters().getString("action");
				if(action.equals("addAccess")){
					
						String appName = requestInfo.getParameters().getString("appName");
						long partnerId = requestInfo.getParameters().getLong("partnerId");
						String appComment = requestInfo.getParameters().getString("appComment");
						
						EApiPartner partner = null;
						if(partnerId != 0){
							partner = EApiPartnerDao.getApiPartner(new Long(partnerId));
						}
						
						if(appName != null && !appName.trim().equals("") && ((partnerId != 0 && partner != null) || (partnerId ==0))){	
							
							EApiPartner apiPartner = EApiPartnerDao.getApiPartner(new Long(partnerId));
							EApiAccess apiAccess = null;
							
							if(apiPartner != null){
								apiAccess = EApiAccess.createApiAccess(appName, EApiPartner.ref(new Long(partnerId)), appComment);
							}else{
								apiAccess = EApiAccess.createApiAccess(appName, appComment);	
							}
							
							if(apiAccess != null){
							
								EApiAccessDao.saveApiAccess(apiAccess);
								Log.logAdmin(requestInfo.getUser().getId(),
										"Admin (userid="
												+ requestInfo.getUser().getId()
												+ ") added an api access (accessid="
												+ apiAccess.getAccessId() + ")");
							
							}
							
						}
						
				}else if(action.equals("saveAccess")){
					long accessId = requestInfo.getParameters().getLong("accessId");
					long partnerId = requestInfo.getParameters().getLong("partnerId");
					EApiAccess apiAccess = EApiAccessDao.getApiAccess(new Long(accessId));
					if(apiAccess != null && apiAccess.isActive()){
						apiAccess.setPartner(EApiPartner.ref(new Long(partnerId)));
						EApiAccessDao.saveApiAccess(apiAccess);
						Log.logAdmin(requestInfo.getUser().getId(),
								"Admin (userid="
										+ requestInfo.getUser().getId()
										+ ") changed the partner of an api access (accessid="
										+ apiAccess.getAccessId() + ")");
					}
				}else if(action.equals("deactivateAccess")){
					long accessId = requestInfo.getParameters().getLong("accessId");
					EApiAccess apiAccess = EApiAccessDao.getApiAccess(new Long(accessId));
					if(apiAccess != null && apiAccess.isActive()){
						apiAccess.setInactive();
						EApiAccessDao.saveApiAccess(apiAccess);
						Log.logAdmin(requestInfo.getUser().getId(),
								"Admin (userid="
										+ requestInfo.getUser().getId()
										+ ") deactivated an api access (accessid="
										+ apiAccess.getAccessId() + ")");
					}

				}
			} catch (ParameterException e) { }
		}		
		
		MainTemplate mainTemplate = new MainTemplate(requestInfo, writer());
		mainTemplate.setTitle("Application API Access");

		Template overviewTemplate = TemplateEngine.getTemplate("admin/site_api_access.html");
		String nextToken = nextToken();
		overviewTemplate.setText("randomKey", nextToken);
		overviewTemplate.setTemplate("adminMenuBar", TemplateEngine.getTemplate("admin/admin_menu_bar.html"));
		
		ArrayList<Template> rowTemplates = new ArrayList<Template>();
		List<EApiAccess> apiAccessList = EApiAccessDao.getApiAccessList();
		
		for(EApiAccess apiAccess:apiAccessList){
			Template template = TemplateEngine.getTemplate("admin/manage/api_access_row.html");
			template.setText("accessid", apiAccess.getAccessId().toString());
			template.setText("randomKey", nextToken);
			
			template.setText("appname", apiAccess.getAppName());
			
			if(apiAccess.getPartner() != null){
				template.setText("partnerId", apiAccess.getPartner().getKey().getId()+"");	
			}else{
				template.setText("partnerId", "NONE");
			}
			
			template.setText("comment", apiAccess.getComment());
			
			template.setText("token", apiAccess.getAccessToken());
			if(!apiAccess.isActive()){
				template.setText("activeStyle", "background-color: #FF5B5B");
				template.setText("activeStyleHide", "display: none;");
			}
			
			rowTemplates.add(template);
		}
		overviewTemplate.setTemplates("accessList", rowTemplates);
		
		mainTemplate.setTemplate("body", overviewTemplate);
		mainTemplate.render(requestInfo.getLocale());

	}

	
	@Override
	protected RequestType requestType() {
		return RequestType.POST_AND_GET;
	}

}
